package com.avcdata.national.network;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.FormattableFlags;
import java.util.HashMap;

public class JsonRunnable implements Runnable {
    private final Handler handler;
    private final String url;
    private String token;
    int position;
    private final String httptype;// 判断发送请求方式
    private HashMap<String, String> maps;
    private final int what;
    private HashMap<String, Object> map;
    private Activity context;

    public JsonRunnable(String httptype, Handler handler, String url,
                        HashMap<String, String> maps, int what, Activity context) {
        this.httptype = httptype;
        this.handler = handler;
        this.url = url;
        this.maps = maps;
        this.token = new SharedPreferencesUtils(context).getSessionId ();
        this.what = what;
        this.context = context;
    }

    public JsonRunnable(Activity context, String httptype, String url, Handler handler,
                        HashMap<String, Object> map, int what) {
        this.httptype = httptype;
        this.handler = handler;
        this.url = url;
        this.map = map;
        this.token = new SharedPreferencesUtils(context).getSessionId ();
        this.what = what;
        this.context = context;
    }

    @Override
    public void run() {
        String result1 = null;
        try {
            Message msg = handler.obtainMessage ();
            BaseLog.e("token","token："+token);
            String result = null;
            try {
                BaseLog.e ( "URL", url + "(" + httptype + ")" );
                if (maps != null) {
                    BaseLog.e ( "params", new Gson().toJson(maps) );
                }
                if (map != null) {
                    BaseLog.e ( "param", new Gson().toJson(map) );
                }
                if (httptype.equals ( "HTTPPOST" )) {
                    if (map != null) {
                        result = HttpRequest.doPost1 ( url, map, token );
                    } else {
                        result = HttpRequest.doPost ( url, maps, token );
                    }
                } else if (httptype.equals ( "HTTPGET" )) {
                    if (map != null) {
                        result = HttpRequest.doGet1 ( url, map, token );
                    } else {
                        result = HttpRequest.doGet ( url, maps, token );
                    }
                } else if(httptype.equals("HTTPPUT")){
                    if (map != null) {
                        result = HttpRequest.doPut1 ( url, map, token );
                    } else {
                        result = HttpRequest.doPut ( url, maps, token );
                    }
                }
                BaseLog.e ( "JSON", result );
                JSONObject json = new JSONObject (result);
                if(result.contains("success")){
                    String status = json.getString ( "success" );
                    if (status.equals ( "true" )) {
                        String backlogJsonStr = json.getString ( "returnValue" );

                        msg.obj = result;
                        msg.what = what;
                    } else {
                        msg.obj = json.getString ( "message" );
                        msg.what = -1;
                    }
                } else {
                    msg.obj = json.getString ( "message" );
                    msg.what = -1;
                }
                if (result.contains ( "secretKey" )) {
                    String secretKey = json.getString ( "secretKey" );
                    if (!(secretKey.equals(new SharedPreferencesUtils(context).getKey()))){
                        msg.obj = "您的账号已在其他设备登录";
                        msg.what = -22;
                    }
                }
                handler.sendMessage ( msg );
            } catch (Exception e) {
                e.printStackTrace ();
                try {
                    JSONObject json = new JSONObject ( result );
                    String error = json.getString ( "error" );
                    if ("invalid_token".equals ( error )) {

                    } else if ("invalid_grant".equals ( error )) {
                        String error_description = json
                                .getString ( "error_description" );
                        if ("Bad credentials".equals ( error_description )) {
                            handler.sendEmptyMessage ( -3 );// 密码错误
                        } else {

                        }
                    } else {
                        e.printStackTrace ();
                        handler.sendEmptyMessage ( -1 );// 连接网络超时或出异常
                    }
                } catch (Exception e1) {
                    e1.printStackTrace ();
                    handler.sendEmptyMessage ( -1 );// 连接网络超时或出异常
                }

            }
        } catch (Exception e) {
            e.printStackTrace ();
            try {
                JSONObject json = new JSONObject ( result1 );
                String error = json.getString ( "error" );
                if ("invalid_token".equals ( error )) {
                } else if ("invalid_grant".equals ( error )) {
                    String error_description = json
                            .getString ( "error_description" );
                    if ("Bad credentials".equals ( error_description )) {
                        handler.sendEmptyMessage ( -3 );// 密码错误
                    } else {
                    }
                } else {
                    handler.sendEmptyMessage ( -1 );// 连接网络超时或出异常
                }
            } catch (Exception e1) {
                e1.printStackTrace ();
                handler.sendEmptyMessage ( -1 );// 连接网络超时或出异常
            }
        }
    }
}